﻿using EquipmentManagement.Application.IServices;
using EquipmentManagement.Domain.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Standard;
using Standard.Dtos;

namespace EquipmentManagement.ApiHost.Controllers
{
    /// <summary>
    /// 设备类型管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceTypeController : ControllerBase
    {
        private readonly IDeviceTypeService _devicetype;
        private readonly ISqlSugarClient _db;

        public DeviceTypeController(IDeviceTypeService devicetype, ISqlSugarClient db)
        {
            _devicetype = devicetype;
            _db = db;
        }
        /// <summary>
        /// 获取列表(分页)
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<DeviceType> GetList([FromQuery] PagingInput paging)
        {
            return _devicetype.GetList(paging);
        }
        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        public ApiResult<int> Add(DeviceType model)
        {
            return _devicetype.Add(model);
        }
        /// <summary>
        /// 删除（逻辑）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> Delete(string id)
        {
            return _devicetype.Delete(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> Update(DeviceType model)
        {
            return _devicetype.Update(model);
        }

        /// <summary>
        /// 导出设备类型清单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportDeviceTypes()
        {
            try
            {
                var query = _db.Queryable<DeviceType>().Where(x => !x.IsDeleted);

                // 列名映射配置（实体属性名 → Excel显示名）
                var columnMappings = new Dictionary<string, string>
                {
                    { "Id", "ID" },
                    { "Code", "类型编码" },
                    { "Name", "设备类型名称" },
                    { "Description", "描述说明" },
                    { "CreatedUserName", "创建人" },
                    { "CreatedTime", "创建时间" },
                    { "UpdateUserName", "更新人" },
                    { "UpdateTime", "更新时间" },
                };

                // 使用改进的ExportService
                var stream = new ExportService().ExportToExcel(
                    query.ToList(),  // 注意：先转换为List
                    columnMappings,
                    "设备类型清单");

                return File(stream, "application/vnd.ms-excel", "DeviceTypes.xls");
            }
            catch (Exception ex)
            {
                return StatusCode(500, "导出过程中发生错误");
            }
        }
        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<DeviceType> Get([FromQuery] string id)
        {
            return _devicetype.Get(id);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<DeviceType>> Gets() 
        {
            return _devicetype.Gets();
        }

    }
}
